/**
 * @Author yinyunqi
 * @date 2018年2月7日
 * @Content 定时任务控制
 */
package com.yongqi.controller.back;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.back.Task;
import com.yongqi.service.back.TimerTaskService;

@Controller
@RequestMapping("/back/timertask")
public class BackTimerTaskController {
    @Autowired
    TimerTaskService timerTaskService;
    /**
     * 后台邮件管理
     * @return 邮件主页
     */
    @RequestMapping("/index")
    public String backTimertaskIndex() {
	return "back/back_timer_task";
    }
    /**
     * 后台邮件修改
     * @return 邮件修改页面
     */
    @RequestMapping("/edit")
    public String backTimertaskEdit(Model model,@RequestParam String id) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("id",id);
	List<Task> task = timerTaskService.backTimerTaskQueryById(map);
	model.addAttribute("timertask", task);
	return "back/back_timer_task_edit";
    }
    /**
     * 后台-定时任务管理-定时任务查询
     */
    @ResponseBody
    @RequestMapping("/query")
    public String backTimertaskQuery(@RequestParam int pageNumber,int pageSize,String taskname) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("taskname", taskname);
    	int total = timerTaskService.backTimerTaskQuery(map).size();
    	PageHelper.startPage(pageNumber, pageSize);
    	List<Task> taskList = timerTaskService.backTimerTaskQuery(map);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("rows", taskList);
	jsonObject.put("total", total);
	return jsonObject.toJSONString();
    }
    /**
     * 后台-定时任务管理-定时任务查询
     */
    @ResponseBody
    @RequestMapping("/save")
    public String backTimertaskSave(@RequestParam String taskname,String cron) {
	System.out.println(taskname+cron);
	Map<String, String> map = new HashMap<String, String>();
	map.put("taskname", taskname);
	map.put("cron", cron);
    	timerTaskService.backTimerTaskEdit(map);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("result", "success");
	return jsonObject.toJSONString();
    }
}

